package bookdemo.MPService.MPimpl;

import bookdemo.MPService.MPBookService;
import bookdemo.dao.BookDao;
import bookdemo.domain.Book;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class MPBookServiceImpl extends ServiceImpl<BookDao, Book> implements MPBookService {
    @Autowired
    @Lazy
    private BookDao bookDao;

    @Override
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id)>0;
    }

    @Override
    public IPage<Book> getname(String name) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)){
            queryWrapper.like("name",name);//模糊搜索
        }
        IPage page = new Page(1,10);
        return bookDao.selectPage(page,queryWrapper);
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        return bookDao.selectPage(page,null);

    }


    @Override
    public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
        LambdaQueryWrapper<Book>  lqw = new LambdaQueryWrapper<Book>();
//        private String type;
//        private String name;
//        private String press;
//        private String author;
//        private String price;
//        private String publication_year;
//        private String imgurl;
//        private String time;
        //条件查询，名字
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        //条件查询，出版社
        lqw.like(Strings.isNotEmpty(book.getPress()),Book::getPress,book.getPress());
        //条件查询，作者
        lqw.like(Strings.isNotEmpty(book.getAuthor()),Book::getAuthor,book.getAuthor());
        //条件查询，价格
        lqw.like(Strings.isNotEmpty(book.getPrice()),Book::getPrice,book.getPrice());
        //条件查询，出版年份
        lqw.like(Strings.isNotEmpty(book.getYear()),Book::getYear,book.getYear());

        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());




        IPage page = new Page(currentPage,pageSize);
        return bookDao.selectPage(page, lqw);

    }
}
